﻿

/*-------------------------------------------------------------------------------------
 * Project Name             : Gems
 * Created By               : santhini
 * Created Date             : 29 Oct 2012
 * Page Function            : insert the records
 * -------------------------------------------------------------------------------------
 * Ref No           Name            Date            Reason
 * R-001            santhini        5 nov 2012    
 * -------------------------------------------------------------------------------------
 */




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

public partial class CategoryMaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    
        if (!IsPostBack)
        {
            if (Request.QueryString["CategoryType_Id"] != null)
            {
                
                String id = Convert.ToString(Request.QueryString["CategoryType_Id"]);
                Int64 ID = Convert.ToInt64(id);
                ViewState["id"] = ID;
                DisplayRecords();
                //Display();
            }

            if (Request.QueryString["TypeMapping_ID"] != null)
            {

                String id = Convert.ToString(Request.QueryString["TypeMapping_ID"]);
                Int64 ID = Convert.ToInt64(id);
                ViewState["TypeMapping_ID"] = ID;

                Display();
            }


           
            LoadDropDown();
            LoadDrop();
            LoadGrid();
        }
        
        
    }
    #region User Defined Function
    public void LoadDropDown()
    {
        //Dropdown list values is selected  from database without using <asp:list items>////


        DataSet dsLoad = DataAccessLayer.GetDataSet("Gems_CommonUsertype");

        if (dsLoad != null && dsLoad.Tables[0].Rows.Count > 0)
        {

            ddlUsertype.DataSource = dsLoad.Tables[0];
            ddlUsertype.DataTextField = "UserType";
            ddlUsertype.DataValueField = "UserType_ID";
            ddlUsertype.DataBind();
        }

    }
    public void LoadDrop()
    {
        //Dropdown list values is selected  from database without using <asp:list items>////


        DataSet dsLoad = DataAccessLayer.GetDataSet("Gems_CommonDiscountType");

        if (dsLoad != null && dsLoad.Tables[0].Rows.Count > 0)
        {

            ddlDiscouttype.DataSource = dsLoad.Tables[0];
            ddlDiscouttype.DataTextField = "DiscountType";
            ddlDiscouttype.DataValueField = "DiscountType_ID";
            ddlDiscouttype.DataBind();
        }


    }
    private void DisplayRecords()
    {

        try
        {

            Hashtable hstbl = new Hashtable();
            hstbl.Add("@CategoryType_Id", ViewState["id"]);
            DataSet ds = DataAccessLayer.GetDataset("GemsCategoryMapping_Fetch_SP", hstbl);

            if (ds.Tables[1].Rows.Count > 0)
            {
                lblcategorytypcode.Text = ds.Tables[1].Rows[0]["TypeCode"].ToString();
                lblcategorytypname.Text = ds.Tables[1].Rows[0]["TypeName"].ToString();

            }
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }


    }
    private void Display()
    {

        try
        {

            Hashtable hstbl = new Hashtable();
            hstbl.Add("@TypeMapping_ID", ViewState["TypeMapping_ID"]);
            DataSet ds = DataAccessLayer.GetDataset("GemsCategoryMapping_Select_SP", hstbl);

            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlUsertype.SelectedItem.Value = ds.Tables[0].Rows[0]["UserType_ID"].ToString();
                ddlUsertype.SelectedItem.Text = ds.Tables[0].Rows[0]["UserType"].ToString();
                ddlDiscouttype.SelectedItem.Value = ds.Tables[0].Rows[0]["DiscountType_ID"].ToString();
                ddlDiscouttype.SelectedItem.Text = ds.Tables[0].Rows[0]["DiscountType"].ToString();
                TextBox1.Text = ds.Tables[0].Rows[0]["Amount"].ToString();

            }
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }


    }
   

    public void SaveRecord()
    {
        try
        {

            Hashtable hstbl1 = new Hashtable();

            hstbl1.Add("@UserType_ID", ddlUsertype.SelectedItem.Value);
            hstbl1.Add("@Discount_ID", ddlDiscouttype.SelectedItem.Value);
            hstbl1.Add("@Amount", TextBox1.Text);
            if (ViewState["TypeMapping_ID"] != null)
            {
                hstbl1.Add("@TypeMapping_ID", Convert.ToInt32(ViewState["TypeMapping_ID"]));
            }

            Int32 intIdentity = DataAccessLayer.ExecuteCommand_RowsAffected("GemsCateogryMapping_InsertUpdate_SP", hstbl1);
            if (intIdentity > 0)
            {
                JSAlert.Show("Insert The Record");
                ViewState["TypeMapping_ID"] = null;
             
                LoadGrid();
            }
            else if (intIdentity == 0)
            {
                JSAlert.Show("User Already Exists");
            }
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }
    public void LoadGrid()
    {

        try
        {
            Hashtable hstbl = new Hashtable();
            if (ViewState["TypeMapping_ID"] != null)
            {
                hstbl.Add("@TypeMapping_ID", ViewState["TypeMapping_ID"]);
            }
            DataSet ds = DataAccessLayer.GetDataset("GemsCategoryMapping_Fetch_SP", hstbl);

            GridView1.DataSource = ds;
            GridView1.DataBind();

        }



        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }

    }
    #endregion

    #region Button Event
    protected void btnadd_Click(object sender, EventArgs e)
    {
        try
        {
            SaveRecord();
            LoadGrid();
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }

    protected void ibtdelete_Click(object sender, EventArgs e)
    {
        try
        {
            GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
            int ID = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Values["TypeMapping_ID"].ToString());
            Hashtable hstbl = new Hashtable();
            hstbl.Add("@TypeMapping_ID", ID);
            Int64 var = DataAccessLayer.ExecuteCommand_RowsAffected("GemsCategoryMapping_Delete_SP", hstbl);
            LoadGrid();
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }

    }
    protected void ibtEdit_Click(object sender, EventArgs e)
    {
        try
        {
            GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
            ViewState["TypeMapping_ID"] = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Values["TypeMapping_ID"].ToString());
            Display();
            //Response.Redirect("~/Master/CategoryMapping.aspx?TypeMapping_ID=" + ID, true);
        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }

    #endregion
}

